package GUI;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import java.awt.Color;
import javax.swing.UIManager;
import java.awt.SystemColor;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

import DataObjects.MetaDataOrder;
import DataObjects.StatusOfTable;
import Messages.MessageGetAllItemsReply;
import Messages.MessageGetAllMenues;
import Messages.MessageGetAllMenuesReply;
import Messages.MessageGetAllTelephoneOrdersForDate;
import Messages.MessageGetAllTelephoneOrdersForDateReply;
import Messages.MessageGetFiredRecipes;
import Messages.MessageGetFiredRecipesReply;
import Messages.MessageGetStatusOfAllTables;
import Messages.MessageGetStatusOfAllTablesReply;

import java.io.IOException;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Vector;

public class PfScheem_All_Tables extends JPanel
{

	private MainMenu mainMenu;
	private JPanel buttonsPanel;
	private Vector<ButtonForTable> vecButtons;

	
	
	public PfScheem_All_Tables(MainMenu mm)
	{
		super();		
		vecButtons=new Vector<ButtonForTable>();
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		this.mainMenu=mm;		
		initialize();
		Thread tableStatus =new Thread(new TableStatus(this));
		tableStatus.setDaemon(true);
		tableStatus.start();
	}
	
	
	
	private void initialize()
	{
	    this.add(getButtonsPanel());	   		
	}	
	
	
	private JPanel getButtonsPanel ()
	{
		buttonsPanel=new JPanel();
		GridLayout grid =new GridLayout(0, 5, 7, 7);
		buttonsPanel.setLayout(grid);
		
		for(int i=0; i<20;i++)
			{
			ButtonForTable button=new ButtonForTable(Integer.toString(i+1),mainMenu);
			button.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					((BorderLayout) mainMenu
							.getContentPane().getLayout())
							.getLayoutComponent(BorderLayout.CENTER).setVisible(false);
					
					ButtonForTable butt=(ButtonForTable)e.getSource();
					MessageGetAllMenues allMenues=new MessageGetAllMenues(butt.getTableNo());
					try {
						mainMenu.ms.client.sendToServer(allMenues);
						MessageGetAllMenuesReply result = (MessageGetAllMenuesReply)mainMenu.ms.client.GetMessage();						
						
						MetaDataOrder metadataOrder=result.getMetaDataOrder();
						mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
						if (result.getBones().size()==0)    //table is free
						mainMenu.getContentPane().add(new PfOpenTable(mainMenu,result.getMenues(),result.getBones(),butt.getTableNo()), BorderLayout.CENTER);	
						else                                //table is opened						
						mainMenu.getContentPane().add(new PfUpdateTable(mainMenu,result.getMenues(),result.getBones(),butt.getTableNo(),result.getMetaDataOrder()), BorderLayout.CENTER);
						mainMenu.getContentPane().validate();
					
					} catch (IOException ei) {
						// TODO Auto-generated catch block
						ei.printStackTrace();
					}		
					
					
				}
			});
			buttonsPanel.add(button);
			vecButtons.add(button);
			}
			
		return buttonsPanel;		
	}
	
	
	
	
	
	private class TableStatus implements Runnable{
		
		private JPanel panel;
		private MessageGetStatusOfAllTablesReply resultTables;
		
		TableStatus (JPanel p)
		{
			panel=p;
		}
		
		
		
		
		
		final Runnable getMessage = new Runnable() {
			public void run() 
			{
				MessageGetStatusOfAllTables msgGetStatus=new MessageGetStatusOfAllTables();				
				try {
					mainMenu.ms.client.sendToServer(msgGetStatus);
					resultTables=(MessageGetStatusOfAllTablesReply)mainMenu.ms.client.GetMessage();				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					resultTables=null;
					System.out.println("in catch TableStatus 1");
				}
				
			}
			};
		
		
		
		
		
		
		public void run()
		{
			int j=0;
			while(j==0)
			{
				if (panel.isVisible()==false)
					return;
				
			try {
				
				
				try {
					SwingUtilities.invokeAndWait(getMessage);
					}
					catch (Exception e) {
					System.out.println("in catch TableStatus 2");
					e.printStackTrace();
					}
				
				Vector<StatusOfTable> resTableStatus=resultTables.getResTableStatus();        // vector of all opened tables
				int vecSize=resTableStatus.size();
				int vecButtonsSize=vecButtons.size();
				for(int i=0;i<vecButtonsSize;i++)
				{
					int indexOfOrderedTable=(-1);
					for(int k=0;k<vecSize;k++)
					{
						int tableNo=Integer.parseInt(resTableStatus.get(k).gettId());
						if(tableNo==i+1)
						{
							indexOfOrderedTable=k;
						}
					}
					
						if(indexOfOrderedTable>-1)   // if table number i+1 is ordered 
						{
							Calendar calOpen=resTableStatus.get(indexOfOrderedTable).getCal();
							Calendar calCurrent=Calendar.getInstance();							
							long time1 = calCurrent.getTimeInMillis() + calCurrent.getTimeZone().getOffset(calCurrent.getTimeInMillis());
					        long time2 = calOpen.getTimeInMillis() + calOpen.getTimeZone().getOffset(calOpen.getTimeInMillis());
					        int result= (int)Math.abs(time1 - time2)/60000;
					        vecButtons.get(i).setMinutsFromOpen(Integer.toString(result));							
							vecButtons.get(i).setStatusOrdered();
							vecButtons.get(i).setNameOfOfficiant(resTableStatus.get(indexOfOrderedTable).getNameOfOfficiant());
							vecButtons.get(i).setNumOfPersons(resTableStatus.get(indexOfOrderedTable).getNoOfPersons());
						}
						
						else
						{
							vecButtons.get(i).cleanButton();							
							
						}
																	
				}
				System.out.println("PfScheem all Tables ");										
				Thread.sleep(5000);               //sleep 5 sec
			}
			catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
		}
}		
}	
	
	

